#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <conio.h>
#include <unistd.h>
#include <windows.h>

#define INIT_PADLEN 4
#define WIDTH 70
#define HEIGHT 20
/*
·------------------------------------------------------------------------->
┊ 0123==================================================================  x
┊ 1  |                                                                 #
┊ 2  |                                                                 #
┊ 3  |                                                                 #
┊ 4                                                                    #
┊ 5                                                                    #
┊ 6                                                                    #
┊ 7                                                                 |  #
┊ 8  |                                                              |  #
┊ 9  |                                                              |  #
┊10  |                      @                                          #
┊11                                                                    #
┊12                                                                    #
┊13                                                                    #
┊14                                                                    #
┊15                                                                    #
┊16                                                                    #
┊17                                                                    #
┊18                                                                    # 
┊19=====================================================================
V  y              地图xy轴如图所示                                             */

char map[HEIGHT][WIDTH];
void init_map(char map[][WIDTH])
{
  // init map
  for (int i = 0; i < HEIGHT; i++)
  {
    // init the first&last line
    if (i == 0 || i == HEIGHT - 1)
    {
      for (int j = 0; j < WIDTH; j++)
      {
        map[i][j] = '=';
      }
    }
    else
    {
      // init the wall on left&right
      map[i][0] = '#';
      map[i][WIDTH - 1] = '#';
      for (int j = 1; j < WIDTH - 1; j++)
      {
        map[i][j] = ' ';
      }
    }
  }
  //init map
}

struct pos
{
  const int x;
  int y;
} B1 = {3, 9},
    B2 = {67, 9};

char ctrl;

int main()
{
  CONSOLE_CURSOR_INFO cursor_info = {1, 0}; /*隐藏光标*/
  SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
  init_map(map);
  B1.y = B2.y = 9;

  while (1)
  {
    int save1y, save2y; 
    save1y = B1.y;
    save2y = B2.y;
    if (kbhit())
    {
      ctrl = getch();
      if (ctrl == 'w' && B1.y != 2) /*p1：ws上下*/
        B1.y -= 1;                  /*p2：85上下*/
      if (ctrl == 's' && B1.y != 17)
        B1.y += 1;
      if (ctrl == '8' && B2.y != 2)
        B2.y -= 1;
      if (ctrl == '5' && B2.y != 17)
        B2.y += 1;
      ctrl = '0';
    }

    for (int i = -1; i != 2; i++)
      map[save1y + i][B1.x] = map[save2y + i][B2.x] = ' '; /*清空原来的板*/
    for (int i = -1; i != 2; i++)
      map[B1.y + i][B1.x] = map[B2.y + i][B2.x] = '|'; /*放上新的板*/

    //打印地图
    char *p;
    p = &map[0][0];
    int a = 0;
    while (p < &map[HEIGHT - 1][WIDTH - 1] + 1)
    {
      if (a == WIDTH)
      {
        printf("\n");
        a = 0;
      }
      printf("%c", *p++);
      a++;
    }

    sleep(0.03);
    system("cls");
  }

}
